package com.logos.jdbc.senior.dao.impl;

import com.logos.jdbc.advanced.entity.Employee;
import com.logos.jdbc.senior.dao.BaseDAO;
import com.logos.jdbc.senior.dao.EmployeeDao;

import java.util.List;

public class EmployeeDaoImpl extends BaseDAO implements EmployeeDao {
    @Override
    public List<Employee> selectAll() {
        try {
            String sql = "select emp_id as empId, emp_name as empName, emp_salary as empSalary, emp_age as empAge from emp";
            List<Employee> employeeList = executeQuery(Employee.class, sql, null);
            return employeeList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Employee selectById(Integer id) {
        try {
            String sql = "select emp_id as empId, emp_name as empName, emp_salary as empSalary, emp_age as empAge from emp where emp_id = ?";
            Employee employee = executeQuerySingle(Employee.class, sql, id);
            return employee;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int insert(Employee employee) {
        try {
            String sql = "insert into emp(emp_name, emp_salary, emp_age ) VALUES (?,?,?)";
            int count = executeUpdate(sql, employee.getEmpName(), employee.getEmpSalary(), employee.getEmpAge());
            return count;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int update(Employee employee) {
        try {
            String sql = "update emp set emp_salary = ? where emp_name = ?";
            int count = executeUpdate(sql, employee.getEmpSalary(), employee.getEmpName());
            return count;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int delete(Employee employee) {
        try {
            String sql = "delete from emp where emp_name = ?";
            int count = executeUpdate(sql, employee.getEmpName());
            return count;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
